package com.puyuba.puyuba.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.puyuba.puyuba.R;
import com.pyb.java.basemodule.BaseActivity;
import com.puyuba.puyuba.module.UserModule;
import com.puyuba.puyuba.network.BaseApis;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.commonutil.ToastHelper;

import com.pyb.java.network.NetBaseApi;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.observers.ResponseObserve;

import androidx.annotation.Nullable;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class SettingActivity extends BaseActivity {
    private ImageView imgBack;
    private TextView logout;
    private LinearLayout llPrivacy;
    private LinearLayout llRegister;
    private LinearLayout llSuggest;


    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        llPrivacy = findViewById(R.id.ll_privacy);
        llRegister = findViewById(R.id.ll_register);
        imgBack = findViewById(R.id.iv_back);
        logout = findViewById(R.id.logout);
        llSuggest = findViewById(R.id.ll_suggest);

        initData();

    }

    private void initData(){
        llPrivacy.setOnClickListener(v -> MainWebActivity.startMainWebActivity(SettingActivity.this, NetBaseApi.PRIVACYPOLICY,"隐私政策"));
        llRegister.setOnClickListener(v -> MainWebActivity.startMainWebActivity(SettingActivity.this, NetBaseApi.REGISETPROTOCOLURL,"用户注册协议"));
        imgBack.setOnClickListener(view -> finish());
        logout.setOnClickListener(view -> logout(CacheUserInfo.getInstance().getId()));
        llSuggest.setOnClickListener(v -> MainWebActivity.startMainWebActivity(SettingActivity.this, BaseApis.addproposeUrl,"投诉与建议"));
    }

    private void logout(int userId){
        showLoadingDialog();
        UserModule.loginOut(userId,new ResponseObserve<Object>(){

                    @Override
                    public void onSuccess(ResponseEntity<Object> responseEntity) {
                        ToastHelper.show("已退出登录");
                        CacheUserInfo.getInstance().clean();
                        startActivity(new Intent(SettingActivity.this, OneClickLoginActivity.class));
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastHelper.show(errorMsg);
                    }


                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void onFinish() {
                        closeLoadingDialog();
                    }
                });
    }

}
